import { Component, OnInit, Input, Output,EventEmitter } from '@angular/core';
import { InitService } from '../../../public/serve/init/init.service';
import { CudTableService } from '../../../public/serve/cud-table/cud-table.service';
import { HintsService } from '../../../public/serve/hints/hints.service';
import { appContentUrl } from '../../content.url';
import { content } from '../../content.data';
import { DatePipe } from '@angular/common';
import { Service } from 'app/server/server';

declare var $:any;
@Component({
  selector: 'app-content-product-add',
  templateUrl: './content-product-add.component.html'
})
export class ContentProductAddComponent implements OnInit {

  @Output() onSubmit = new EventEmitter<void>();

  constructor(
    public initService: InitService,
    private request: CudTableService,
    private datePipe: DatePipe,
    private hintsService: HintsService,
    private server : Service,
  ) { }

  ngOnInit() {
  }

  public initial:boolean = true;

  /**
   * 新增提交
   * @param info 
   */
  _onSubmit(info: any){
    info.token = content.retrieval.token;
    info.dataSource = content.retrieval.dataSource;//需手动选择
    info.categoryId = content.retrieval.categoryId;//需手动选择
    info.productType = content.retrieval.productType;
    info.fileFormat = content.retrieval.fileFormat;
    info.filePath = content.retrieval.filePath;
    info.createTime = this.datePipe.transform(new Date(), 'yyyy-MM-dd HH:mm:ss');
    info.createBy = content.retrieval.createBy;
    info.author = content.retrieval.author;

    this.request
    .postRequest(this.server.baseUrl + appContentUrl.addProduct,info)
    .then(res=>{
      if(res.result_code === '0000'){
        this.onSubmit.emit();
        content.retrieval.author = '';
      }

      this.hintsService.messages(res.message,res.result_code);
      this._init();
      return;
    })
    .catch((err: any) => {
      this.hintsService.messages('error','error');
      this._init();
      return;
    });
  }

  /**
   * 初始化弹窗
   */
  private _init(){
    this.initService._init();
    $("#contentProductAdd").modal('hide');
  }

  getAddAuthorPersons($event) {
    $('#author').val($event);
    this.onSubmit.emit();
  }

  /**
   * 取消初始化
   */
  _choseInit(){
    content.retrieval.author = '';
    this.onSubmit.emit();
  }

}
